package com.example.javapracticaltraining.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.javapracticaltraining.entity.dto.HealthReport;
import com.example.javapracticaltraining.entity.pojo.AiHealthReport;
import com.example.javapracticaltraining.mapper.AiHealthReportMapper;
import com.example.javapracticaltraining.service.AiHealthReportService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 健康报告服务实现类
 */
@Service
public class AiHealthReportServiceImpl extends ServiceImpl<AiHealthReportMapper, AiHealthReport> implements AiHealthReportService {

    @Override
    public AiHealthReport saveHealthReport(HealthReport healthReport, String targetRole) {
        AiHealthReport aiHealthReport = new AiHealthReport();
        
        // 设置基本信息
        aiHealthReport.setElderlyId(healthReport.getElderlyId());
        aiHealthReport.setReportType(healthReport.getReportType());
        aiHealthReport.setGeneratedDate(new Date()); // 使用当前时间作为生成时间
        aiHealthReport.setContent(healthReport.getContent());
        aiHealthReport.setSummary(healthReport.getSummary());
        aiHealthReport.setHealthTrends(healthReport.getHealthTrends());
        aiHealthReport.setCarePoints(healthReport.getCarePoints());
        aiHealthReport.setRiskAssessment(healthReport.getRiskAssessment());
        aiHealthReport.setRecommendations(healthReport.getRecommendations());
        aiHealthReport.setTargetRole(targetRole);
        
        // 保存到数据库
        this.save(aiHealthReport);
        
        return aiHealthReport;
    }

    @Override
    public List<AiHealthReport> getReportsByElderlyId(Integer elderlyId) {
        LambdaQueryWrapper<AiHealthReport> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AiHealthReport::getElderlyId, elderlyId);
        queryWrapper.orderByDesc(AiHealthReport::getGeneratedDate); // 按生成时间降序排序
        
        return this.list(queryWrapper);
    }

    @Override
    public List<AiHealthReport> getReportsByType(String reportType) {
        LambdaQueryWrapper<AiHealthReport> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AiHealthReport::getReportType, reportType);
        queryWrapper.orderByDesc(AiHealthReport::getGeneratedDate); // 按生成时间降序排序
        
        return this.list(queryWrapper);
    }

    @Override
    public List<AiHealthReport> getReportsByElderlyIdAndType(Integer elderlyId, String reportType) {
        LambdaQueryWrapper<AiHealthReport> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AiHealthReport::getElderlyId, elderlyId);
        queryWrapper.eq(AiHealthReport::getReportType, reportType);
        queryWrapper.orderByDesc(AiHealthReport::getGeneratedDate); // 按生成时间降序排序
        
        return this.list(queryWrapper);
    }
}